package jsyf.oa.web.tmpevent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jsyf.com.util.FastJsonConvert;
import jsyf.oa.web.socket.MyWebSocketHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.tmpevent.EventFacade;
import org.springframework.web.socket.TextMessage;

import java.util.List;

@Api(value = "临时事项接口", description = "临时事项接口")
@Controller
@RequestMapping("/event")
public class EventController {
    @Resource
    private EventFacade eventFacade;
    @Autowired
    private MyWebSocketHandler myWebSocketHandler;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = eventFacade.save(jsonData);
        //发送消息
        sendMessage(jsonObject.getString("message"));
        jsonObject.remove("message");
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }

    @RequestMapping(value = "/getUnDoCount.json", method = RequestMethod.GET)
    public void getUnDoCount(HttpServletResponse response, @RequestParam String userId) {
        String count = eventFacade.getUnDoCount(userId);
        JSONObject records = new JSONObject();
        records.put("count", count);
        if (count != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }

    @RequestMapping(value = "/getByPage.json", method = RequestMethod.POST)
    public void getByPage(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String userId = obj.getString("userId");
        String requestPage = obj.getString("requestPage");
        String pageSize = obj.getString("pageSize");
        String key = obj.getString("key");
        String companyId = obj.getString("companyId");
        String auditFlag = obj.getString("auditFlag");
        String type = obj.getString("type");
        String createUserName = obj.getString("createUserName");
        if (StringUtils.isEmpty(userId)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("用户Id为空"));
            return;
        }
        if (StringUtils.isEmpty(requestPage)) {
            requestPage = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = Integer.MAX_VALUE + "";
        }
        JSONObject records = this.eventFacade.getByPage(userId, key, companyId, auditFlag, type, createUserName, Integer.parseInt(requestPage), Integer.parseInt(pageSize));
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }

    @RequestMapping(value = "/getById.json", method = RequestMethod.POST)
    public void getById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String id = obj.getString("id");
        if (StringUtils.isBlank(id)) {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
            return;
        }
        JSONObject records = this.eventFacade.getById(id);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        JSONObject result = eventFacade.applyAudit(jsonObject.getString("id"), jsonObject.getString("userId"), jsonObject.getString("auditFlag"), jsonObject.getString("advise"));
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        sendMessage(result.getString("message"));
        result.remove("message");
        if (flag) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("审核失败"));
        }
    }

    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void removeById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        Boolean result = eventFacade.removeById(jsonObject.getString("id"));
        JSONObject obj = new JSONObject();
        obj.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/copierReturn.json", method = RequestMethod.POST)
    public void copierReturn(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        JSONObject result = eventFacade.copierReturn(jsonObject.getString("id"), jsonObject.getString("copierReturnReason"));
        boolean flag = Boolean.parseBoolean(result.getString("flag"));
        //发送消息
        sendMessage(result.getString("message"));
        result.remove("message");
        JSONObject obj = new JSONObject();
        obj.put("success", flag);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(obj));
    }

    @RequestMapping(value = "/getFirstAuditor.json", method = RequestMethod.POST)
    public void getFirstAuditor(HttpServletResponse response) {
        JSONObject result = eventFacade.getFirstAuditor();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

    private void sendMessage(String jsonData) {
        List<JSONObject> jsonObjectList = null;
        if (!StringUtils.isBlank(jsonData)) {
            jsonObjectList = FastJsonConvert.convertJSONToArray(jsonData, JSONObject.class);
        }
        if (null != jsonObjectList && jsonObjectList.size() > 0) {
            TextMessage textMessage = new TextMessage(BaseUtils.succsessArrHourTimeRespond(jsonObjectList), true);
            try {
                myWebSocketHandler.sendMsgToAllUsers(textMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
